/**
 * 提取字符串中所有的$()和${}格式的字符串值
 * @param input 输入的字符串
 * @returns 提取出的不重复字符串值数组
 * @example
 * extractDollarStrings('$("hello") ${"world"} $("hello")')
 * // returns ["hello", "world"]
 */
const borderColorRegex = /border-(?:[a-z]+-)?(?:[a-z]+-)?color-([a-zA-Z0-9#]+)/g;
const extractBorderColors = (className: string) => {
  const matches = [...className.matchAll(borderColorRegex)];
  console.log(
    '提取到的边框颜色值:',
    matches.map((match) => match[1]),
  );
  return matches.map((match) => match[1]);
};

const borderWidthRegex = /border-(?:[a-z]+-)?(?:[a-z]+-)?width-([0-9]+)/g;
const extractBorderWidths = (className: string) => {
  const matches = [...className.matchAll(borderWidthRegex)];
  return matches.map((match) => match[1]);
};

const borderStyleRegex = /border-(?:[a-z]+-)?(?:[a-z]+-)?style-([a-z]+)/g;
const extractBorderStyles = (className: string) => {
  const matches = [...className.matchAll(borderStyleRegex)];
  return matches.map((match) => match[1]);
};

const combineBorderStyles = (colors: string[], widths: string[], styles: string[]) => {
  return colors
    .map((color, index) => {
      const width = widths[index] || '1px';
      const style = styles[index] || 'solid';
      return `${width} ${style} ${color}`;
    })
    .join(' ');
};

const extractDollarStrings = (input: string): string[] => {
  // 匹配 $() 或 ${} 中的字符串字面量，支持转义引号
  const dollarRegex = /\$\((\\?["'])(.*?)(?<!\\)\1\)|\$\{(\\?["'])(.*?)(?<!\\)\3\}/g;
  const matches = [...input.matchAll(dollarRegex)];
  // 处理可能的转义字符
  const results = matches
    .map((match) => {
      const str = match[2] || match[4];
      return str?.replace(/\\"/g, '"').replace(/\\'/g, "'");
    })
    .filter(Boolean);
  return [...new Set(results)];
};

export { extractDollarStrings };
